<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['id']){
	$_SESSION['hotelid']=$_RQ['id'];
}
$return['status']=$this->order_status;
$return['paytype']=get_pay_types();
$return['ispay']=array('0'=>'未支付','1'=>'已支付');
$return['dai_type']=$this->dai_type;
$hotelid=$_SESSION['hotelid'];
if($_RQ['op']=='list'){
	$this->model('task')->orderup(true);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'hotelid'=>$hotelid);
	if(!empty($_RQ['order'])){
		$params['order']=trim($_RQ['order']);
		$return['params']['order']=$_RQ['order'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
    //$fields=array('id','order','name','intime','hotelid','roomid','ispay','paytype','fee','mid','createtime','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_jd_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
		$this->model('model')->uporderstatus($v);
		$v['hotel']=pdo_get('jy_jd_hotel',array('id'=>$v['hotelid']));
		$v['room']=pdo_get('jy_jd_room',array('id'=>$v['roomid']));
		$v['statustitle']=$this->order_status[$v['status']];
		$endtime=strtotime($v['intime'])+$v['day']*24*3600;
		$v['endtime']=date('Y-m-d',$endtime);
		if(!$v['status'] && ($v['ispay'] || $v['dai_type'])){
			$v['quren']=1;	
		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
			array('op'=>'dai','title'=>'第三方报单'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'order','title'=>'订单号','type'=>'string'),
			array('name'=>'room.title','title'=>'预定的房间','type'=>'string'),
			array('name'=>'num','title'=>'房间数量','type'=>'string'),
			array('type'=>'img_string','title'=>'入住人',
			'data'=>array(
				array('title'=>'入住人：','name'=>'name'),
				array('title'=>'入住电话：','name'=>'mobile')
				)
			),
			array('type'=>'img_string','title'=>'入住时间',
			'data'=>array(
				array('title'=>'入住时间：','name'=>'intime'),
				array('title'=>'离店时间：','name'=>'endtime')
				)
			),
			array('type'=>'img_string','title'=>'费用',
			'data'=>array(
				array('title'=>'订单金额：','name'=>'fee'),
				array('title'=>'包含押金：','name'=>'yafee')
				)
			),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('name'=>'dai_type','title'=>'预定平台','type'=>'radio','options'=>'dai_type'),			
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'付款方式','type'=>'radio','options'=>'paytype'),	
		),
		'action'=>array(
			array('type'=>'modal','action'=>'jy_jd/order/setstatus/','title'=>'设置状态'),
			array('type'=>'post','action'=>'jy_jd/order/queren/','title'=>'确认预定','show'=>array('status'=>0,'quren'=>1)),
			array('type'=>'modal','action'=>'jy_jd/order/shquxiao','title'=>'申请取消','show'=>array('status'=>1)),
			array('type'=>'modal','action'=>'jy_jd/order/quxiao','title'=>'同意申请退款','show'=>array('status'=>2)),
			array('type'=>'modal','action'=>'jy_jd/order/pay','title'=>'确认退款','show'=>array('status'=>3)),
			array('type'=>'post','action'=>'jy_jd/order/ruzhu','title'=>'确认入住','show'=>array('status'=>1)),
			array('type'=>'modal','action'=>'jy_jd/order/tuifang','title'=>'退房','show'=>array('status'=>6)),
			array('type'=>'modal','action'=>'jy_jd/order/item','title'=>'订单详情'),
		),),
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
			array('name'=>'nickname','width'=>15,'title'=>'用户昵称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'订单状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setstatus'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$status=intval($post['status']);
		$id=intval($post['id']);
		$update=array('status'=>$status,'ispay'=>intval($post['ispay']));
		pdo_update('jy_jd_order',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('设置成功！','','/order/list/');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),array('id','ispay','order','status','createtime'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置状态',
	);
	$htmls['edit']=array(
			array('type'=>'string','title'=>'订单号','name'=>'order','read'=>true),
			array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'status'),
			array('type'=>'radio','title'=>'是否支付','name'=>'ispay','options'=>'ispay'),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='item'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if(!$item){
		exi('指定订单不存在~','error');
	}
	$item['hotel']=pdo_get('jy_jd_hotel',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['hotelid']));
	$item['room']=pdo_get('jy_jd_room',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['roomid']));
	$item['statustitle']=$this->order_status[$item['status']];
	$endtime=strtotime($item['intime'])+$item['num']*24*3600;
	$item['endtime']=date('Y-m-d',$endtime);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'look'=>array(
			array('name'=>'order','title'=>'订单号','type'=>'string'),
			array('name'=>'room.title','title'=>'预定的房间','type'=>'string'),
			array('title'=>'房间数量：','name'=>'num','type'=>'string'),
			array('title'=>'入住人：','name'=>'name','type'=>'string'),
			array('title'=>'入住电话：','name'=>'mobile','type'=>'string'),
			array('title'=>'入住时间：','name'=>'intime','type'=>'string'),
			array('title'=>'离店时间：','name'=>'endtime','type'=>'string'),
			array('title'=>'订单金额：','name'=>'fee','type'=>'string'),
			array('title'=>'订单押金：','name'=>'yafee','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'付款方式','type'=>'radio','options'=>'paytype'),
		),
	);
	exi($return);
}
if($_RQ['op']=='dai'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$room=pdo_get('jy_jd_room',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['roomid']));
		if(!$post['roomid']){
			exi('请选择预定的房间','error');	
		}
		if(!$post['intime']){
		exi('请选择入住日期','error');	
		}
		if(!$post['day']){
			exi('请填写入住时长','error');	
		}
		if(!$post['num']){
			exi('请选择房间数量','error');	
		}
		$innum=$this->model('model')->check_innum($room,$post['intime'],$post['day'],$post['num']);
		if($post['num']==1){
			$allday=$post['intime'];
		}else{
			$allday=';'.$post['intime'].';';
			$time=strtotime($post['intime']);
			for($i=1;$i<$post['day'];$i++){
				$allday.=date("Y-m-d",$time+$i*24*3600).';';
			}
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'hotelid'=>$hotelid,
			'roomid'=>$post['roomid'],
			'name'=>$post['name'],
			'mobile'=>$post['mobile'],
			'day'=>$post['day'],
			'num'=>$post['num'],
			'intime'=>$post['intime'],
			'allday'=>$allday,
			'order'=>get_ordersn('jy_jd_order','order'),
			'fee'=>$post['fee'],
			'dai_type'=>$post['dai_type'],
			'status'=>1,
			'createtime'=>TIMESTAMP		
		);
		if($hotel['userid']){
			$data['userid']=$hotel['userid'];
		}
		pdo_insert('jy_jd_order',$data);
		exi('报单成功！','','/order/list/');	
	}
	$return['rooms']=pdo_getall('jy_jd_room',array('uniacid'=>$_SESSION['uniacid'],'hotelid'=>$hotelid));
	$return['item']=array();
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('name'=>'dai_type','title'=>'第三方平台','type'=>'radio','options'=>'dai_type'),
			array('name'=>'roomid','title'=>'预定的房间','type'=>'select','options'=>'rooms'),
			array('title'=>'房间数量：','name'=>'num','type'=>'string'),
			array('title'=>'入住人姓名：','name'=>'name','type'=>'string'),
			array('title'=>'入住电话：','name'=>'mobile','type'=>'string'),
			array('title'=>'入住时间：','name'=>'intime','type'=>'time'),
			array('title'=>'入住天数：','name'=>'day','type'=>'string'),
			array('title'=>'订单金额：','name'=>'fee','type'=>'string'),
		),
	);
	exi($return);
}
if($_RQ['op']=='queren'){
		$id=intval($_RQ['id']);
		$order=pdo_get('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		if($order['status']!=0 || (!$order['dai_type'] && !$order['ispay'])){
			exi('只有预定状态并且已支付的订单才能确认','error','list');	
		}
		pdo_update('jy_jd_order',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$this->model('notice')->notice_querenorder($order);
		exi('确认预定成功','','/order/list/id='.$order['hotelid']);			
}
if($_RQ['op']=='shquxiao'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$status=intval($post['status']);
		$id=intval($post['id']);
		$order=pdo_get('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));	
		if($order['status']!=1){
			exi('只有未入住的订单才能申请取消','error','list');	
		}
		pdo_update('jy_jd_order',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('申请取消成功','','/order/list/id='.$order['hotelid']);	
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),array('id','mid','fee','ispay','order','status','createtime'));
	$item['nickname']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'nickname');	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'申请取消订单',
	);
	$htmls['edit']=array(
			array('type'=>'string','title'=>'订单号','name'=>'order','read'=>true),
			array('name'=>'nickname','title'=>'会员昵称','type'=>'string','read'=>true),
			array('name'=>'fee','title'=>'金额','type'=>'string','read'=>true),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status','read'=>true),	
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay','read'=>true),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);		
}
if($_RQ['op']=='quxiao'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$status=intval($post['status']);
		$id=intval($post['id']);
		$order=pdo_get('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));	
		if($order['status']!=2){
			exi('只有申请退款的订单，才能确认退款','error','list');	
		}
		pdo_update('jy_jd_order',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功','','/order/list/id='.$order['hotelid']);	
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),array('id','mid','fee','ispay','order','status','createtime'));
	$item['nickname']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'nickname');	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'确认退款订单',
	);
	$htmls['edit']=array(
			array('type'=>'string','title'=>'订单号','name'=>'order','read'=>true),
			array('name'=>'nickname','title'=>'会员昵称','type'=>'string','read'=>true),
			array('name'=>'fee','title'=>'金额','type'=>'string','read'=>true),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status','read'=>true),	
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay','read'=>true),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);		
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'hotelid'=>$hotelid);
	if(!empty($_RQ['order'])){
		$params['order']=trim($_RQ['order']);
		$return['params']['order']=$_RQ['order'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_jd_order',$params);
	if(count($order)>5000){
		exi('订单数量大于5000，请修改搜索条件！','error');
	}
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
		$this->model('model')->uporderstatus($v);
		$v['hotel']=pdo_get('jy_jd_hotel',array('id'=>$v['hotelid']));
		$v['room']=pdo_getcolumn('jy_jd_room',array('id'=>$v['roomid']),'title');
		$v['statustitle']=$this->order_status[$v['status']];
		$endtime=strtotime($v['intime'])+$v['day']*24*3600;
		$v['endtime']=date('Y-m-d',$endtime);
		$v['dai_type']=$this->dai_type[$v['dai_type']];
		$v['ispay']=$ispay[$v['ispay']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['createtime']=date('Y-m-d',$v['createtime']);
	}
	$header=array(
		'order'=>'订单号',
		'room'=>'预定的房间',
		'name'=>'入住人',
		'intime'=>'入住时间',
		'endtime'=>'离店时间',
		'fee'=>'费用',
		'statustitle'=>'订单状态',
		'dai_type'=>'预定平台',
		'createtime'=>'下单时间',
		'ispay'=>'是否支付',
		'paytype'=>'付款方式',
	);
	cfc('tools')->export2excel($header,$order,'订单列表');
}
if($_RQ['op']=='pay'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->model('refund')->refund_pay($id);
		exi('打款成功！','','/order/list/id='.$post['hotelid']);
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if(!$item){
		exi('指定退款订单不存在~','error');
	}
	$member=model('core/core')->member_show($item['mid']);
	$item['wxgid']=pdo_getcolumn('core_fans',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'wxgid');
	if($member['from_os']==1){
		$item['apptitle']=pdo_getcolumn('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['wxgid']),'title');
	}else{
		$item['apptitle']=pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['wxgid']),'title');
	}
	$item['paytype']=get_pay_types()[$item['paytype']];	
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：如果打款会员是<red>小程序</red>会员，会显示所属《小程序》，否则可能会打款不成功!默认打款使用的是公众号！'),
			array('name'=>'apptitle','title'=>'所属','type'=>'copy'),
			array('type'=>'copy','name'=>'paytype','title'=>'退款类型'),
			array('type'=>'copy','name'=>'fee','title'=>'退款金额'),
			array('type'=>'copy','name'=>'fee','title'=>'退款到账金额'),
		),
	);
	exi($return);
}
if($_RQ['op']=='tuifang'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if($post['yafee']>0){
			$this->model('refund')->refund_payyafee($id);
		}
		pdo_update('jy_jd_order',array('status'=>5),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$this->model('notice')->notice_tuifang($post);
		exi('退房成功','success','/order/list/id='.$post['hotelid']);
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($item['status']!=6){
		exi('已入住的订单才能完成退房！','error');	
	}
	$item['roomtitle']=pdo_getcolumn('jy_jd_room',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['roomid']),'title');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：确定客户入住时间已到，要退房吗?并确认退费押金（原路返回）！'),
			array('name'=>'roomtitle','title'=>'预定的房间','type'=>'copy'),
			array('type'=>'copy','name'=>'yafee','title'=>'退押金金额'),
		),
	);
	exi($return);		
}
if($_RQ['op']=='ruzhu'){
		$id=intval($_RQ['id']);
		$order=pdo_get('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));		
		if($order['status']!=1){
			exi('已预定成功的订单才能入住！','error','list');	
		}
		pdo_update('jy_jd_order',array('status'=>6),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$this->model('notice')->notice_ruzhu($order);
		exi('入住成功','success','/order/list/id='.$order['hotelid']);			
}
if($_RQ['op']=='del'){
	exi('暂时不支持删除订单！','error');
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>